import { get, post, getBlob } from '@/utils/http';
import {
  GetInventoryParams,
  GetInventoryResult,
  InventoryNotesSaveParams,
  GetInventoryOptionListResult,
  IBaseInfoParams,
  IBaseInfoDetail,
  IFacilityResult,
  ISourceFactorParams,
  IFacilitySaveParams,
  IFacilityDetailParams,
  IFacilityDetail,
  IFacilitySourceSaveParams,
  IFacilitySourceDetail,
  IFacilityCountRes,
  IInventoryOptionsParams,
  IEmsDevice,
  GetListFillTaskResult
} from './model/inventoryModel';

import { BasicPagingListResult } from '@/apis/basicModel';

enum Api {
  // 监测计划分页列表
  GET_INVENTORY_PAGE_LIST = '/inventory/emissionInventory/inventoryPage',
  // 已提交的碳排放监测计划列表查
  GET_RELEASED_PLAN_LIST = '/inventory/emissionInventory/listReleased',
  // 发布监测计划
  PUBLISH_INVENTORY = '/inventory/emissionInventory/inventoryRelease',
  // 删除监测计划
  DELETE_INVENTORY = '/inventory/emissionInventory/inventoryDelete',
  // 复制监测计划
  COPY_INVENTORY = '/inventory/emissionInventory/inventoryCopySave',
  // 生成清单文件
  GENERATE_INVENTORY_FILE = '/inventory/emissionInventory/download',
  // 备注信息保存接口
  INVENTORY_NOTES_SAVE = '/inventory/emissionInventory/inventoryNotesSave',
  // 碳排放监测计划基本信息保存接口
  BASE_INFO_SAVE = '/inventory/emissionInventory/inventorySave',
  // 监测计划基本信息编辑
  BASE_INFO_EDIT = '/inventory/emissionInventory/inventoryEdit',
  // 基本信息查询接口
  BASE_INFO_QUERY = '/inventory/emissionInventory/inventoryInfo',
  //  碳排放边界和设施信息分页查询接口
  FACILITY_LIST = '/inventory/emissionInventory/facility/facilityList',
  // 碳排放设施的目录查询
  SOURCE_CATALOG = '/inventory/emissionFramework/sourceCatalog',
  // 碳排放设施的排放源、排放因子查询
  SOURCE_FACTOR_LIST = '/inventory/emissionFramework/sourceFactorList',
  // 碳排放监测计划的核算边界保存接口
  BOUNDARY_SAVE = '/inventory/emissionInventory/inventoryBoundarySave',
  // 碳排放设施信息保存接口
  FACILITY_SAVE = '/inventory/emissionInventory/facility/facilitySave',
  // 碳排放设施信息编辑接口
  FACILITY_EDIT = '/inventory/emissionInventory/facility/facilityEdit',
  // 碳排放设施信息查询
  FACILITY_QUERY = '/inventory/emissionInventory/facility/facilityInfo',
  // 碳排放设施信息删除
  FACILITY_DEL = '/inventory/emissionInventory/facility/facilityDelete',
  // 第三步 碳排放源信息填报保存
  FACILITY_SOURCE_SAVE = '/inventory/emissionInventory/facility/sourceSave',
  // 第三步 碳排放源信息铲鲟
  FACILITY_SOURCE_QUERY = '/inventory/emissionInventory/facility/sourceInfo',
  // 碳排放源统计接口
  FACILITY_COUNT = '/inventory/emissionInventory/facility/facilityCount',
  //  更换监测计划
  CHANGE_EMP = '/inventory/task/changeEmp',
  // 监测计划撤销发布
  CANCEL_RELEASE = '/inventory/emissionInventory/inventoryCancelRelease',
  // 根据工厂ID和仪表类型，获取ems仪表列表
  GET_EMS_OPTIONS = '/thirdability/emsDevice/getEmsDeviceByPlantId',
  // 关联ems数据
  RELATE_EMS = '/inventory/adTask/relateEmsRecord',
  // 查询监测计划对应的填报任务
  GET_LIST_FILL_TASK = '/inventory/adTask/listFillTask',
  // 撤回填报任务
  POST_WITHDRAW_TASK = '/inventory/adTask/withdrawTask',
  // 重新下发填报任务
  POST_DISPATCH_TASK = '/inventory/adTask/dispatchTask',
  // 重启填报任务（任务将会重新开启，填报数据不会删除）
  POST_RESTART_TASK = '/inventory/adTask/restartTask',
  // 已发布监测计划修改审核人
  POST_INVENTORY_UPDATE_AUDITOR = '/inventory/emissionInventory/inventoryUpdateAuditor',
  // 更新排放设施
  UPDATE_FACILITY = '/inventory/emissionInventory/facility/refreshFacility'
}

// 监测计划列表
export const getInventoryList = (
  params: GetInventoryParams
): Promise<BasicPagingListResult<GetInventoryResult>> => get(Api.GET_INVENTORY_PAGE_LIST, params);

// 发布监测计划
export const publishInventory = (inventoryNo: string) =>
  post(Api.PUBLISH_INVENTORY, { inventoryNo });
// 撤销监测计划
export const cancelRelease = (inventoryNo: string) => post(Api.CANCEL_RELEASE, { inventoryNo });

// 删除监测计划
export const deleteInventory = (inventoryNo: string) => post(Api.DELETE_INVENTORY, { inventoryNo });

// 复制监测计划
export const copyInventory = (inventoryNo: string) => post(Api.COPY_INVENTORY, { inventoryNo });

// 备注信息保存接口
export const inventoryNotesSave = (params: InventoryNotesSaveParams): Promise<boolean> =>
  post(Api.INVENTORY_NOTES_SAVE, params);

// 生成清单文件
export const generateInventoryFile = (params: { inventoryNo: string }) =>
  getBlob(Api.GENERATE_INVENTORY_FILE, params);

// 监测计划option列表
export const getInventoryOptionList = (
  params: IInventoryOptionsParams
): Promise<GetInventoryOptionListResult[]> => get(Api.GET_RELEASED_PLAN_LIST, params);

// 保存基本信息
export const saveBaseInfo = (params: IBaseInfoParams): Promise<string> =>
  post(Api.BASE_INFO_SAVE, params);

// 编辑基本信息
export const postEditBaseInfo = (params: IBaseInfoParams) => post(Api.BASE_INFO_EDIT, params);

// 基本信息查询
export const queryBaseInfo = (inventoryNo: string): Promise<IBaseInfoDetail> =>
  get(Api.BASE_INFO_QUERY, { inventoryNo });

export const getFacilityList = (inventoryNo: string): Promise<IFacilityResult[]> =>
  get(Api.FACILITY_LIST, { inventoryNo });

// 4.1 碳排放设施的目录查询
export const getSourceCatalog = (level: 3 | 2) => get(Api.SOURCE_CATALOG, { level });

// 4.2 碳排放设施的排放源、排放因子查询
export const getSourceFactorList = (params: ISourceFactorParams) =>
  get(Api.SOURCE_FACTOR_LIST, params);

// REMOVE
export const planBoundarySave = (inventoryNo: string, boundary: string): Promise<string> =>
  post(Api.BOUNDARY_SAVE, { inventoryNo, boundary });

// 碳排放设施信息保存接口
export const planFacilitySave = (params: IFacilitySaveParams): Promise<void> =>
  post(Api.FACILITY_SAVE, params);

// 碳排放设施信息编辑接口
export const planFacilityEdit = (params: IFacilitySaveParams): Promise<void> =>
  post(Api.FACILITY_EDIT, params);

// 2.5 碳排放设施信息删除接口
export const planFacilityDel = (params: IFacilityDetailParams): Promise<void> =>
  post(Api.FACILITY_DEL, params);
// 2.4 碳排放设施信息明细查询接口
export const getFacilityDetail = (params: IFacilityDetailParams): Promise<IFacilityDetail> =>
  get(Api.FACILITY_QUERY, params);

// 碳排放源信息填报保存接口 REMOVE
export const planFacilitySourceSave = (params: IFacilitySourceSaveParams): Promise<void> =>
  post(Api.FACILITY_SOURCE_SAVE, params);
//  排放源信息明细查询接口 REMOVE
export const getFacilitySourceDetail = (
  params: IFacilityDetailParams
): Promise<IFacilitySourceDetail> => get(Api.FACILITY_SOURCE_QUERY, params);

// 2.3 碳排放源统计接口 REMOVE
export const getFacilityCount = (inventoryNo: string): Promise<IFacilityCountRes> =>
  get(Api.FACILITY_COUNT, { inventoryNo });

export const getEmsDevice = (meterType: string, plantId: string): Promise<IEmsDevice> =>
  get(Api.GET_EMS_OPTIONS, { meterType, plantId });
// 关联ems
export const relateEms = (fillTaskDataNo: string): Promise<IFacilityCountRes> =>
  get(Api.RELATE_EMS, { fillTaskDataNo });

/**
 * 查询监测计划对应的填报任务
 */
export const getListFillTask = (inventoryNo: string): Promise<GetListFillTaskResult[]> =>
  get(Api.GET_LIST_FILL_TASK, { inventoryNo });

/**
 * 撤回填报任务
 */
export const postWithdrawTask = (taskNo: string): Promise<void> =>
  post(Api.POST_WITHDRAW_TASK, { taskNo });

/**
 * 重新下发填报任务
 */
export const postDispatchTask = (taskNo: string): Promise<void> =>
  post(Api.POST_DISPATCH_TASK, { taskNo });

/**
 * 重启填报任务（任务将会重新开启，填报数据不会删除）
 */
export const postRestartTask = (taskNo: string): Promise<void> =>
  post(Api.POST_RESTART_TASK, { taskNo });

/**
 * 已发布监测计划修改审核人
 */
export const postInventoryUpdateAuditor = (
  inventoryNo: string
): Promise<{
  auditorId: string;
  auditor: string;
  auditorEmail: string;
}> => post(Api.POST_INVENTORY_UPDATE_AUDITOR, { inventoryNo });

export const updateFacility = (inventoryNo: string, facilityNoList: string[]): Promise<void> =>
  post(Api.UPDATE_FACILITY, { inventoryNo, facilityNoList });
